package com.example.asus_pc.myshop.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.asus_pc.myshop.R;
import com.example.asus_pc.myshop.adapter.AbsCommonAdapter;
import com.example.asus_pc.myshop.adapter.AbsViewHolder;
import com.example.asus_pc.myshop.bean.VegaBean;
import com.example.asus_pc.myshop.utils.SharedPreUtils;

import java.util.List;

public class ChooseVegaActivity extends AppCompatActivity implements View.OnClickListener{

    TextView tvLeft;
    TextView tvTitle;
    TextView tvRight;
    ListView lvChoose;
    AbsCommonAdapter<VegaBean> chooseAdapter;
    private List<VegaBean> vegaBeans;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_vega);

        initViews();
        setListeners();

    }


    @Override
    protected void onResume() {
        super.onResume();
        String vegas = SharedPreUtils.getString(SharedPreUtils.VEGA_CONTENT, "");
        if(!vegas.equals("")){
            vegaBeans = VegaBean.jsonToList(vegas);
            chooseAdapter.addData(vegaBeans);
        }
    }

    private void setListeners() {

        tvRight.setOnClickListener(this);
        lvChoose.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                VegaBean bean = vegaBeans.get(position);
                if(bean.getIsSelected()==1){
                    bean.setIsSelected(0);
                    lvChoose.setItemChecked(position,false);
                }else {
                    bean.setIsSelected(1);
                    lvChoose.setItemChecked(position,true);
                }
            }
        });
    }

    private void initViews() {
        tvLeft = (TextView) findViewById(R.id.tv_left);
        tvRight = (TextView) findViewById(R.id.tv_right);
        tvTitle = (TextView) findViewById(R.id.tv_title);

        lvChoose = (ListView) findViewById(R.id.lv_choose_vega);
        chooseAdapter = new AbsCommonAdapter<VegaBean>(this,R.layout.item_show_vega) {
            @Override
            public void convert(AbsViewHolder helper, VegaBean item, int pos) {
                helper.setText(R.id.tv_order_number,pos+1+"");
                helper.setText(R.id.tv_vega_name,item.getVegaName());
                helper.setText(R.id.tv_total_price,item.getTotalPrice()+"");
                helper.setText(R.id.tv_splite_price,item.getSplitPrice()+"");
                if(item.getIsSelected()==1){
                    lvChoose.setItemChecked(pos,true);
                }else {
                    lvChoose.setItemChecked(pos,false);
                }
            }
        };
        lvChoose.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        lvChoose.setAdapter(chooseAdapter);

        tvLeft.setVisibility(View.GONE);
        tvTitle.setText("选择品种");
        tvRight.setText("完成");

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_right:
                List<VegaBean> datas = chooseAdapter.getDatas();
                String dataStr = VegaBean.listToJson(datas);
                SharedPreUtils.putString(SharedPreUtils.VEGA_CONTENT,dataStr);
                finish();
                break;
        }
    }
}
